package drawables;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import enemy.Enemy;
import game.Camera;

public class EnemyDrawable implements Drawable<Enemy>{
	
	private static final int LASER_TIME = 40;
	
	private Image sprite, sight, laser;
	
	private float x, y, rotation, sightedness, distanceToPlayer;
	private int laserStart = 0;
	
	public EnemyDrawable() throws SlickException{
		sprite = new Image("resources/enemy/enemy.png");
		sprite.setCenterOfRotation(75, 75);
		sight = new Image("resources/enemy/enemy_sighting.png");
		sight.setCenterOfRotation(32, 32);
		laser = new Image("resources/enemy/enemy_laser.png");
		laser.setCenterOfRotation(16, 32);
	}

	@Override
	public void draw(Graphics g, Camera camera) {
		sprite.setRotation(rotation);
		g.setDrawMode(Graphics.MODE_ADD);
		g.drawImage(sprite, x-75+camera.getOffsetX(), y-75+camera.getOffsetY());
		if(sightedness > 0 && laserStart <= LASER_TIME){
			sight.setRotation(rotation);
			float xOff = x+camera.getOffsetX();
			float yOff = y+camera.getOffsetY();
			float flicker = (float)Math.random()*0.5f + 0.5f;
			flicker *= 1 - ((float)laserStart/LASER_TIME);
			Color filter = new Color(flicker, flicker, flicker);
			g.drawImage(sight, xOff-32, yOff-200*sightedness, xOff+32, yOff+200*sightedness, 0, 0, 64, 64, filter);
			if(sightedness >= 1){
				laser.setRotation(rotation);
				laser.draw(xOff-16, yOff-distanceToPlayer, xOff+16, yOff, 0, 0, 32, 32, filter);
				if(laserStart == 0)
					laserStart = 1;
			}
		}
		if(laserStart > 0 && laserStart <= LASER_TIME)
			laserStart++;
		g.setDrawMode(Graphics.MODE_NORMAL);
	}

	@Override
	public void update(Enemy enemy) {
		x = enemy.getX();
		y = enemy.getY();
		rotation = enemy.getRotation();
		sightedness = enemy.getSightedness();
		distanceToPlayer = enemy.distanceToPlayer();
	}

}
